/*************************************************************************
	> File Name: Daemon.hpp
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/10/15 18:31
 ************************************************************************/

#pragma once

#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

namespace halil
{
    void Daemon()
    {
        signal(SIGPIPE, SIG_IGN);
        signal(SIGCHLD, SIG_IGN);
        if (fork() > 0) {
            exit(0);
        }
        setsid();
        int devnull = open("dev/null", O_RDONLY | O_WRONLY);
        if (devnull > 0) {
            dup2(0, devnull);
            dup2(1, devnull);
            dup2(2, devnull);
            close(devnull);
        }
    }
}
